/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2017
 */

package com.database.management.web;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.database.management.base.BaseDao;
import com.database.management.common.Result;
import com.database.management.domain.SqlStatement;
import com.database.management.service.CommandService;
import com.database.management.service.IDbConnectionService;
import com.database.management.service.ISqlStatementService;
import com.database.management.service.IndexService;

@Controller
@RequestMapping("/sqlStatement")
public class SqlStatementController {
	private Logger log = LoggerFactory.getLogger(SqlStatementController.class);
	@Resource
	private ISqlStatementService iSqlStatementService;
	@Autowired
	private IDbConnectionService iDbConnectionService;
	
	@ResponseBody
	@RequestMapping(path="/save",method=RequestMethod.POST)
	public Result save(SqlStatement sqlStatement) {
		Result result = new Result();
		if(sqlStatement!=null){
			sqlStatement.setCreatedate(new Date());
			SqlStatement sql = iSqlStatementService.insert(sqlStatement);
			if (sql.getId()  != null) {
				result.setSuccess("保存成功");
			} else {
				result.setError("保存失败");
			}
		}else{
			result.setError("前台传递参数错误！");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(path="/update",method=RequestMethod.POST)
	public Result update(SqlStatement sqlStatement) {
		Result result = new Result();
		if(sqlStatement!=null && sqlStatement.getId() != null){
			SqlStatement sql = iSqlStatementService.update(sqlStatement);
			if (sql.getId()  != null) {
				result.setSuccess("保存成功");
			} else {
				result.setError("保存失败");
			}
		}else{
			result.setError("前台传递参数错误！");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(path="/delete",method=RequestMethod.POST)
	public Result delete(Integer id) {
		Result result = new Result();
		if(id!=null ){
			iSqlStatementService.delete(id);
			result.setSuccess("删除成功");
		}else{
			result.setError("前台传递参数错误！");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/findAll")
	public List<SqlStatement> findAll(SqlStatement sqlStatement) {
		return iSqlStatementService.findAll(Example.of(sqlStatement),new Sort("heat"));
	}
	
	@ResponseBody
	@RequestMapping("/findPage")
	public Result findPage(SqlStatement sqlStatement,Integer page, @RequestParam(defaultValue = "20")Integer size) {
		Result result = new Result();
		if(sqlStatement!=null ){
			result.setSuccess("查询成功");
			PageRequest pageable =  new PageRequest(page, size);
			result.setResult(iSqlStatementService.findPage(Example.of(sqlStatement),pageable));
		}else{
			result.setError("前台传递参数错误！");
		}
		return result;
	}

	
	@ResponseBody
	@RequestMapping("/connection")
	public Result connection(Integer id) {
		IndexService indexService = new IndexService(id);
		Result result = new Result();
		BaseDao.addConnection(iDbConnectionService.findOne(id));
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/execute")
	public Object execute(Integer id,String sql) {
		CommandService commandService = new CommandService(id);
		try {
			if(sql.toLowerCase().indexOf("select") != -1){
					return commandService.find(sql);
			}else{
				return commandService.update(sql);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping("/getAllTableName")
	public List getAllTableName(Integer id) {
		CommandService commandService = new CommandService(id);
		try {
			return commandService.getAllTableName();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}

